﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Linq;

using NUnit.Framework;

using Refix.RepositoryBrowser.Models;

namespace Refix.RepositoryBrowser.Tests.Models
{
    [TestFixture]
    public class RepositoryModelTests
    {
        [Test]
        public void SetRepository_SetsValueAndRaisesEvent()
        {
            var model = new RepositoryModel();
            var counter = new EventCounter();
            var item = new RepositoryItem();

            model.RepositoryChanged += counter.Handler;

            model.Repository = item;

            Assert.AreEqual(1, counter.Counter,
                            "Setting the Repository property should have raised the RepositoryChanged event exactly once.");
            Assert.AreSame(item, model.Repository,
                           "The Repository property was not set to the expected object.");
        }

        [Test]
        public void SetSelectedItem_SetsValueAndRaisesEvent()
        {
            var model = new RepositoryModel();
            var counter = new EventCounter();
            var item = new RepositoryItem();

            model.RepositoryItemSelected += counter.Handler;

            model.SelectedItem = item;

            Assert.AreEqual(1, counter.Counter,
                            "Setting the SelectedItem property should have raised the RepositoryItemSelected event exactly once.");
            Assert.AreSame(item, model.SelectedItem,
                           "The SelectedItem property was not set to the expected object.");
            Assert.AreSame(item, ((RepositoryItemEventArgs)counter.LastEventArgs).Item,
                           "The event arguments didn't contain the child when the SelectedItem property was set.");
        }

        [Test]
        public void AddRepositoryItem_SetsParentChildRelationshipAndRaisesEvent()
        {
            var model = new RepositoryModel();
            var counter = new EventCounter();
            var parent = new RepositoryItem();
            var child = new RepositoryItem();

            model.RepositoryItemAdded += counter.Handler;

            model.AddRepositoryItem(parent, child);

            Assert.AreEqual(1, counter.Counter,
                            "Calling the AddRepositoryItem property should have raised the RepositoryItemAdded event exactly once.");
            Assert.AreSame(parent, child.Parent,
                           "The AddRepositoryItem method didn't set the parent property.");
            Assert.Contains(child, parent.Children.ToList(),
                            "The AddRepositoryItem method didn't set the child in the collection.");
            Assert.AreSame(child, ((RepositoryItemEventArgs)counter.LastEventArgs).Item,
                           "The event arguments didn't contain the child when the AddRepositoryItem method was called.");
        }

        [Test]
        public void RemoveRepositoryItem_ClearsParentChildRelationshipAndRaisesEvent()
        {
            var model = new RepositoryModel();
            var counter = new EventCounter();
            var parent = new RepositoryItem();
            var child = new RepositoryItem();
            parent.Children.Add(child);

            model.RepositoryItemRemoved += counter.Handler;

            model.RemoveRepositoryItem(parent, child);

            Assert.AreEqual(1, counter.Counter,
                            "Calling the RemoveRepositoryItem property should have raised the RepositoryItemAdded event exactly once.");
            Assert.False(parent.Children.Contains(child),
                            "The RemoveRepositoryItem method didn't remove the child from the collection.");
            Assert.AreSame(child, ((RepositoryItemEventArgs)counter.LastEventArgs).Item,
                           "The event arguments didn't contain the child when the RemoveRepositoryItem method was called.");
        }

        [Test]
        public void ReportStatus_RaisesEvent()
        {
            var model = new RepositoryModel();
            var counter = new EventCounter();
            string status = "xxx";

            model.StatusReported += counter.Handler;

            model.ReportStatus(status);

            Assert.AreEqual(1, counter.Counter,
                            "Calling the ReportStatus method should have raised the StatusReported event exactly once.");
            Assert.AreEqual(status, ((ValueEventArgs<string>)counter.LastEventArgs).Value,
                           "The event arguments didn't contain the status when the ReportStatus property was set.");
        }

        public class EventCounter
        {
            private int _counter;
            private EventArgs _lastEventArgs;

            public int Counter
            {
                get { return _counter;  }
            }

            public EventArgs LastEventArgs
            {
                get { return _lastEventArgs; }
            }

            public void Handler(object sender, EventArgs e)
            {
                ++_counter;
                _lastEventArgs = e;
            }
        }
    }
}